app.controller("z-shiftPermission-c", [ '$scope','$rootScope','$z-http','$sce','z-shiftPermission-s','z-edit-menu-s','z-employee-s','z-corporation-s','z-department-s','$filter',
		function($scope,$rootScope,$zhttp,$sce,Service,MenuService,EmployeeService,CorporationService,DepartmentService,$filter) {
		$scope.viewModel = {};
		$scope.editModel = {};
		$scope.viewModel.perPage = "10";
		$scope.viewModel.rowCount = "1";
		//编辑使用
		$scope.editModel = {};
		$scope.editModel.title="";
		// 获取当天日期
		$scope.viewModel.effectiveDate = new Date();
		
		//查询页面名称
		MenuService.getMenuByUrl({url:'shiftPermission'},
		function(data){	
			$scope.viewModel.menuList=data;
		},function(error) {});
		
		//加载数据
		$scope.viewModel.bindData = function(){
			$scope.viewModel.isLoading=true;
			//获取基本信息
			Service.getShiftPermissionList({
				pageCount : $scope.viewModel.perPage,
				pageNo : $scope.viewModel.rowCount,
				shiftName:$scope.viewModel.shiftName
			},
			function(data){
				$scope.viewModel.dataList = data.shiftPermissionBeanList;
				$scope.viewModel.totalData = data.pageTotal;
				$scope.viewModel.isLoading=false;
			},function(error) {
				$scope.viewModel.isLoading=false;
			});
		};
		
		$scope.viewModel.bindData();
		
		//加载
		$scope.viewModel.load=function(){
			$scope.viewModel.show=true;
		};
		//关闭加载
		$scope.viewModel.unLoad=function(){
			$scope.viewModel.show=false;
		};
		
		//新增
		$scope.viewModel.add = function(){
			$scope.viewModel.load();
			$scope.editModel.type = true;
			$scope.editModel.title="新增班次权限";
			$scope.editModel.shiftPermission={};
			$scope.editModel.shiftPermission.employeeList=[];
			$scope.editModel.shiftPermission.departmentList=[];
			//验证不能为空
			$scope.editForm.shiftName.$dirty=false;
		};
		//修正
		$scope.viewModel.update=function(shiftPermission){
			$scope.viewModel.load();
			$scope.editModel.type = false;
			$scope.editModel.title="修改班次权限";
			$scope.editModel.shiftPermission=angular.copy(shiftPermission);
		};
		//验证
		$scope.viewModel.requiredForm= function() {
            if (!$scope.editForm.$valid) {
            	if(!$scope.editForm.shiftName.$dirty){
            		$scope.editForm.shiftName.$dirty=true;
            	}
                return true;
            }else{
            	if($scope.editModel.shiftPermission.employeeList.length==0){
            		$rootScope.alertWarning("请选择分配人员。");
            		return true;
            	}else if($scope.editModel.shiftPermission.departmentList.length==0){
            		$rootScope.alertWarning("请选择分配部门。");
            		return true;
            	}else{
            		return false;
            	}
            }
        };
		//保存
		$scope.viewModel.save = function(){
			if($scope.viewModel.requiredForm()){
				return;
			}
			$scope.viewModel.isLoading = true;
			if($scope.editModel.type){
				Service.saveShiftPermission($scope.editModel.shiftPermission,
					function(data){
						$scope.viewModel.unLoad();
						$scope.viewModel.bindData();
						$rootScope.alertInfo("新增成功。");
						$scope.viewModel.isLoading = false;
					},function(error) {
						$rootScope.alertError(error.fault.message);
						$scope.viewModel.isLoading = false;
					});
			}else{
				Service.updateShiftPermission($scope.editModel.shiftPermission,
					function(data){
						$scope.viewModel.unLoad();
						$scope.viewModel.bindData();
						$rootScope.alertInfo("修改成功。");
						$scope.viewModel.isLoading = false;
					},function(error) {
						$rootScope.alertError(error.fault.message);
						$scope.viewModel.isLoading = false;
					});
			}
		};
		
		// 删除
		$scope.viewModel.del = function(shiftPermission){
			$rootScope.alertConfirm("确定是否删除吗？",function() {
				$scope.viewModel.isLoading=true;
				Service.deleteShiftPermission({
					shiftId:shiftPermission.shiftId
				},function(){
					$scope.viewModel.bindData();
					$scope.viewModel.isLoading=false;
					$rootScope.alertInfo("删除成功。");
				},function(error){
					$scope.viewModel.isLoading=false;
					$rootScope.alertError(error.fault.message);
				});
			});
		};
		
		// 删除选择人员
		$scope.editModel.delEmployee = function(employee){
			$rootScope.alertConfirm("确定是否删除人员吗？",function() {
				for(var i = 0; i < $scope.editModel.shiftPermission.employeeList.length; i++){
					if($scope.editModel.shiftPermission.employeeList[i].employeeId == employee.employeeId){
						$scope.editModel.shiftPermission.employeeList.splice(i,1);
					}
				}
			});
		};
		
		// 删除选择部门
		$scope.editModel.delDepartment = function(department){
			$rootScope.alertConfirm("确定是否分配部门吗？",function() {
				for(var i = 0; i < $scope.editModel.shiftPermission.departmentList.length; i++){
					if($scope.editModel.shiftPermission.departmentList[i].departmentId == department.departmentId){
						$scope.editModel.shiftPermission.departmentList.splice(i,1);
					}
				}
			});
		};
		
		/*******选择人员*********/
		$scope.viewEmployeeModel={};
		$scope.viewEmployeeModel.perPage = "10";
		$scope.viewEmployeeModel.rowCount = "1";

		//加载信息
		$scope.viewEmployeeModel.bindCorporationData = function(){
			//查询公司
			CorporationService.getCorporationList({
				fromDate:$scope.viewModel.effectiveDate,
			},
			function(data){
				var site = [];  
		        $.each(data, function(index, values) {  
		            site.push({  
		                label : "("+values.corporationCode+")"+values.corporationName,  
		                value : values.corporationCode  
		            });  
		        });  
		        $('#spCorporation').multiselect('dataprovider',site);
			},function(error) {
				
			});
		};
		
		// 人员选择按钮
		$scope.viewEmployeeModel.selectEmployee = function(){
			$scope.viewEmployeeModel.show=true;
			$scope.viewEmployeeModel.title="选择员工";
			$scope.viewEmployeeModel.employeeIsSeparated=null;
			$scope.viewEmployeeModel.regionType=null;
			$scope.viewEmployeeModel.employeeNo=null;
			$scope.viewEmployeeModel.roleName=null;
			$scope.viewEmployeeModel.departmentList=[];
			$scope.viewEmployeeModel.corporationList=[];
			$scope.viewEmployeeModel.employeeList=[];
			// 临时选中人员集合
			$scope.viewEmployeeModel.checkboxDataList=[];
			$scope.viewEmployeeModel.level=null;
			//下拉多选框长度
			$scope.multiSelectMaxHeight=300;
			
			// 添加选中的人员到临时选中人员集合
			$scope.viewEmployeeModel.checkboxDataList=[];
			for(var i=0;i<$scope.editModel.shiftPermission.employeeList.length;i++){
				var data=$scope.editModel.shiftPermission.employeeList[i];
				$scope.viewEmployeeModel.checkboxDataList.push(data);
			}
			
			// 加载公司下拉框
			$scope.viewEmployeeModel.bindCorporationData();
			
			/*******使用为下拉带勾选控件*********/
			//员工级别
			$('#splevel').multiselect({
				includeSelectAllOption: true,
				enableFiltering: true,
				delimiterText: '; ',
	            maxHeight: $scope.multiSelectMaxHeight
	        });
			//公司
			$('#spCorporation').multiselect({
				includeSelectAllOption: true,
				enableFiltering: true,
				delimiterText: '; ',
	            maxHeight: $scope.multiSelectMaxHeight
	        });
			//公司选中事件
			$("#spCorporation").change(function(){  
				var value=$(this).val();
			    var data={};
			    data.corporationCodeList=value;
			    data.fromDate = $scope.viewModel.effectiveDate;
			    if(value==null){
			    	var site = [];  
			    	$('#spDepartmentName').multiselect('dataprovider',site);
			    	$scope.viewEmployeeModel.departmentList=[];
			    }else{
			    	//查询部门
				    DepartmentService.getDepartmentListByCorporationCodeList(data,
					function(data){
				    	var site = [];  
				        $.each(data, function(index, values) {  
				            site.push({  
				                label : "("+values.departmentCode+")"+values.departmentName,  
				                value : values.departmentCode  
				            });  
				        });  
				        $('#spDepartmentName').multiselect('dataprovider',site);
					},function(error) {
						
					});
			    }
	        });  
			
			//部门
			$('#spDepartmentName').multiselect({
				includeSelectAllOption: true,
				enableFiltering: true,
				delimiterText: '; ',
	            maxHeight: $scope.multiSelectMaxHeight
	        });
			
			// 获取人员列表
			EmployeeService.getEmployeeList({
				rowCount : $scope.viewEmployeeModel.perPage,
				perPage : $scope.viewEmployeeModel.rowCount,
				fromDate:$scope.viewModel.effectiveDate,
			},
			function(data){
				$scope.viewEmployeeModel.dataEmployeeList = data.employeeBeanList;
				$scope.viewEmployeeModel.totalData = data.pageTotal;
				$scope.viewEmployeeModel.isLoading=false;
			},function(error) {
				$rootScope.alertError(error.fault.message);
				$scope.viewEmployeeModel.isLoading=false;
			});
		};
		
		//关闭加载
		$scope.viewEmployeeModel.unLoad=function(){
			$scope.viewEmployeeModel.show=false;
		};
		
		//是否选中
		$scope.viewEmployeeModel.isInAssignmentArr=function(employee){
			for(var i=0;i<$scope.viewEmployeeModel.checkboxDataList.length;i++){
				if($scope.viewEmployeeModel.checkboxDataList[i].employeeId == employee.employeeId){
					return true;
				}
			}
			return false;
		};
		
		//加载数据
		$scope.viewEmployeeModel.bindData = function(){
			$scope.viewEmployeeModel.employeeList=[];
			if($scope.viewEmployeeModel.employeeNo!=null){
				var employeeNoData = $scope.viewEmployeeModel.employeeNo.split("\n");
				for(var i=0;i<employeeNoData.length;i++){
					$scope.viewEmployeeModel.employeeList.push(employeeNoData[i]);
				}
			}
			$scope.viewModel.isLoading=true;
			//获取基本信息
			EmployeeService.getEmployeeList({
				employeeList:$scope.viewEmployeeModel.employeeList,
				departmentList:$scope.viewEmployeeModel.departmentList,
				corporationList:$scope.viewEmployeeModel.corporationList,
				employeeLevelList:$scope.viewEmployeeModel.employeeLevelList,
				employeeIsSeparated:"0",
				perPage:$scope.viewEmployeeModel.rowCount,
				rowCount:$scope.viewEmployeeModel.perPage,
				fromDate:$scope.viewModel.effectiveDate,
			},
			function(data){
				$scope.viewEmployeeModel.dataEmployeeList = data.employeeBeanList;
				$scope.viewEmployeeModel.totalData = data.pageTotal;
				$scope.viewModel.isLoading=false;
			},function(error) {
				$scope.viewModel.isLoading=false;
			});
		};
		
		// 点击选择指定人员按钮
		$scope.viewEmployeeModel.employee = function(employee){
			var data={};
			data.employeeId=employee.employeeId;
			data.employeeNo=employee.employeeNo;
			data.chineseName=employee.chineseName;
			data.englishName=employee.englishName;
			var type=true;
			// 判断是否存在
			for(var i=0;i<$scope.viewEmployeeModel.checkboxDataList.length;i++){
				if($scope.viewEmployeeModel.checkboxDataList[i].employeeId==data.employeeId){
					$scope.viewEmployeeModel.checkboxDataList.splice(i,1);
					type=false;
					break;
				}
			}
			if(type){
				$scope.viewEmployeeModel.checkboxDataList.push(data);
			}
		};
		
		//选择人员保存
		$scope.viewEmployeeModel.saveEmployee=function(){
			$scope.editModel.shiftPermission.employeeList=[];
			// 将选中人员添加到人员集合
			for(var i=0;i<$scope.viewEmployeeModel.checkboxDataList.length;i++){
				var data=$scope.viewEmployeeModel.checkboxDataList[i];
				$scope.editModel.shiftPermission.employeeList.push(data);
			}
			$scope.viewEmployeeModel.unLoad();
		};
		
		/***********************/
		
		
		/*******选择部门*********/
		$scope.viewDepartmentModel={};
		$scope.viewDepartmentModel.perPage="10";
		$scope.viewDepartmentModel.rowCount="1";
		
		//加载信息
		$scope.viewDepartmentModel.bindCorporationData = function(){
			//查询公司
			CorporationService.getCorporationList({
				fromDate:$scope.viewModel.effectiveDate,
			},
			function(data){
				var site = [];  
		        $.each(data, function(index, values) {  
		            site.push({  
		                label : "("+values.corporationCode+")"+values.corporationName,  
		                value : values.corporationCode  
		            });  
		        });  
		        $('#sdCorporation').multiselect('dataprovider',site);
			},function(error) {
				
			});
		};
		
		// 部门选择按钮
		$scope.viewDepartmentModel.selectDepartment = function(){
			$scope.viewDepartmentModel.dataList={};
			$scope.viewDepartmentModel.departmentName="";
			$scope.viewDepartmentModel.show=true;
			$scope.viewDepartmentModel.corporationList=[];
			//下拉多选框长度
			$scope.multiSelectMaxHeight=300;
			
			// 添加选中的部门到临时选中部门集合
			$scope.viewDepartmentModel.checkboxDataList=[];
			for(var i=0;i<$scope.editModel.shiftPermission.departmentList.length;i++){
				var data=$scope.editModel.shiftPermission.departmentList[i];
				$scope.viewDepartmentModel.checkboxDataList.push(data);
			}
			
			// 加载公司下拉框
			$scope.viewDepartmentModel.bindCorporationData();
			
			//公司
			$('#sdCorporation').multiselect({
				includeSelectAllOption: true,
				enableFiltering: true,
				delimiterText: '; ',
	            maxHeight: $scope.multiSelectMaxHeight
	        });
			
			// 加载部门列表
			$scope.viewDepartmentModel.bindData();
		};
		
		$scope.viewDepartmentModel.bindData = function(){
			$scope.viewDepartmentModel.isLoading=true;
			//获取基本信息
			DepartmentService.getDepartmentList({
				corporationCodeList:$scope.viewDepartmentModel.corporationList,
				pageNo:$scope.viewDepartmentModel.rowCount,
				pageCount:$scope.viewDepartmentModel.perPage,
				departmentName:$scope.viewDepartmentModel.departmentName,
				fromDate:$scope.viewModel.effectiveDate,
			},
			function(data){
				$scope.viewDepartmentModel.dataList = data.departmentBeanList;
				$scope.viewDepartmentModel.totalData = data.pageTotal;
				$scope.viewDepartmentModel.isLoading=false;
			},function(error) {
				$scope.viewDepartmentModel.isLoading=false;
			});
		};
		
		$scope.viewDepartmentModel.load=function(){
			$scope.viewDepartmentModel.show=true;
		};
		
		//关闭加载
		$scope.viewDepartmentModel.unLoad=function(){
			$scope.viewDepartmentModel.show=false;
		};
		
		// 点击选择指定部门按钮
		$scope.viewDepartmentModel.department = function(department){
			var data={};
			data.departmentId=department.departmentId;
			data.departmentCode=department.departmentCode;
			data.departmentName=department.departmentName;
			data.corporationCode=department.corporationCode;
			data.corporationName=department.corporationName;
			var type=true;
			// 判断是否存在
			for(var i=0;i<$scope.viewDepartmentModel.checkboxDataList.length;i++){
				if($scope.viewDepartmentModel.checkboxDataList[i].departmentId==data.departmentId){
					$scope.viewDepartmentModel.checkboxDataList.splice(i,1);
					type=false;
					break;
				}
			}
			if(type){
				$scope.viewDepartmentModel.checkboxDataList.push(data);
			}
		};
		
		//是否选中
		$scope.viewDepartmentModel.isInAssignmentArr=function(department){
			for(var i=0;i<$scope.viewDepartmentModel.checkboxDataList.length;i++){
				if($scope.viewDepartmentModel.checkboxDataList[i].departmentId == department.departmentId){
					return true;
				}
			}
			return false;
		};
		
		//选择部门保存
		$scope.viewDepartmentModel.saveDepartment=function(){
			$scope.editModel.shiftPermission.departmentList=[];
			// 将选中部门添加到部门集合
			for(var i=0;i<$scope.viewDepartmentModel.checkboxDataList.length;i++){
				var data=$scope.viewDepartmentModel.checkboxDataList[i];
				$scope.editModel.shiftPermission.departmentList.push(data);
			}
			$scope.viewDepartmentModel.unLoad();
		};
		/***********************/
} ]);